package main.java.com.lee.procon;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/20
 */
public class Producer<T> implements Runnable {
    public static final int producer_gap = 200;
    public static AtomicInteger TURN = new AtomicInteger(0);
    public static final AtomicInteger producer_no = new AtomicInteger(0);
    String name = null;
    Callable<T> action = null;
    int gap = producer_gap;

    public Producer(Callable<T> action, int gap) {
        this.action = action;
        this.gap = gap;
        this.name = "生产者-" + producer_no.incrementAndGet();
    }
    @Override
    public void run() {
        while (true) {
            try {
                Object result = action.call();
                if (null != result) {
                    System.out.println("第" + TURN.get() + "轮生产: " + result);
                }
                Thread.sleep(gap);
                TURN.incrementAndGet();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
